<?php

namespace app\agent\controller;

use app\agent\model\User;
use app\agent\library\Backend;
use think\Config;
use app\common\model\BetsRecord;
use app\common\model\Config as ConfigModel;
use think\Db;
use app\common\model\Bets;
use app\agent\model\Record;


/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    protected $cids = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->cids = User::where('pid',$this->auth->id)->column('id');
    }

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');

        $sysconfig = ConfigModel::where(['group'=>'basic','name'=>'web_url'])->value('value');

        $betnum = BetsRecord::sum('money');

        $todaybetnum = BetsRecord::whereTime('createtime', 'today')->sum('money');
        $code = Db::name('agent')->where('user_id',$this->auth->id)->value('code');

        $totaluser = User::where('pid',$this->auth->id)->count();
        $todayusersignup = User::where('pid',$this->auth->id)->whereTime('createtime', 'today')->count();


        $starttime = \fast\Date::unixtime('day', 0, 'begin');
        $endtime = \fast\Date::unixtime('day', 0, 'end');
        //当天投注人数
        $betnumsbyuserid = Bets::whereIn('user_id',$this->cids)
            ->whereTime('addtime','between',[$starttime,$endtime])
            ->group('user_id')
            ->select();
        //每日首存人数 当天注册的人数
        $nowcid = User::where('pid',$this->auth->id)->column('id');//注册总人数
        $rechargenumsbyuseridtoday = Record::where('type','in',[1,7,8])->whereIn('user_id',$nowcid)->where(function ($query) use($starttime,$endtime) {
            $query->whereTime('createtime','between',[$starttime,$endtime]);
        })->group('user_id')->column('user_id'); //今日充值

        $rechargenumsbyuseridbefor = Record::where('type','in',[1,7,8])->whereIn('user_id',$nowcid)->where(function ($query) use($starttime,$endtime) {
            $query->whereTime('createtime','<',$starttime);//今日以前充值
        })->group('user_id')->column('user_id');
        $rechargenumsbytoday = 0;
        foreach ( $rechargenumsbyuseridtoday as $k=>$v ){
            if( !in_array($v,$rechargenumsbyuseridbefor) ){
                $rechargenumsbytoday++;
            }
        }
        $this->view->assign([
            'todayrechargenumsbyuserid' => $rechargenumsbytoday,//每日首存人数
            'todaybetnumsbyuserid'      => count($betnumsbyuserid),
            'totaluser'        => $totaluser,
            'betnum'       => $betnum, //投注量
            'todaybetnum'   => $todaybetnum, //今日投注量
            'todayusersignup'  => $todayusersignup,
            'todayorder'       => 2324,
            'unsettleorder'    => 132,
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode,
            'inviteUrls'       => json_decode($sysconfig,true),
            'inviteCode'       => $code
        ]);
        return $this->view->fetch();
    }

}
